package com.yc.web.fillter;

import com.yc.bean.Resuser;
import com.yc.commons.RedisHelper;
import com.yc.utils.YcConstants;
import redis.clients.jedis.Jedis;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Calendar;

/**
 * @author zwei
 * @create 2024-06-18 20:40
 */
@WebFilter(value = "/user.action")
public class AddictedUsersFilter extends BaseFilter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(servletRequest,servletResponse);
        //只针对  login操作的响应做出处理
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        String op=req.getParameter("op");
        if ("login".equals(op)){
            //说明是login操作
            //如果登录成功  在session中一定有resuser
            HttpSession session=req.getSession();
            if (session.getAttribute(YcConstants.RESUSER)!=null){
                //登录成功  取出今天是周几  存到redis中去
                Resuser resuser= (Resuser) session.getAttribute(YcConstants.RESUSER);
                long userid=resuser.getUserid();
                Jedis jedis= RedisHelper.getRedisInstance();
                //业务 取出服务器的时间  周几  作为键来存
                int weekday= Calendar.getInstance().get(Calendar.DAY_OF_WEEK); //周日是1 周一是2...周六是7
                jedis.setbit(weekday+"",userid,true);//将uid这一值设为1
                jedis.close();
            }
        }
    }

    @Override
    public void destroy() {

    }
}
